package com.tf.six.controller;

import com.tf.six.util.RedisKeyUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Set;

@RestController
@Tag(name = "Redis 缓存", description = "直接查询 热门商品排行")
@RequestMapping("/api/redis")
public class RedisTestController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/hot-products")
    public Set<Object> getHotProductsFromRedis() {
        // 直接返回 Redis ZSET 中的全部数据（商品ID + 分值）
        return Collections.singleton(redisTemplate.opsForZSet().reverseRangeWithScores(RedisKeyUtil.HOT_PRODUCT_KEY, 0, -1));
    }
}